﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewData["Title"] = "修改维修单";
    Layout = null;
}
@Html.AntiForgeryToken()
@using Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改维修单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" media="all">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
        }

        .form-container {
            max-width: 1000px;
            margin: 0 auto;
            background: white;
            border-radius: 12px;
            box-shadow: 0 8px 32px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .form-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .form-title {
            font-size: 28px;
            font-weight: 600;
            margin: 0;
        }

        .form-subtitle {
            margin-top: 8px;
            opacity: 0.9;
            font-size: 14px;
        }

        .form-content {
            padding: 40px;
        }

        .form-section {
            margin-bottom: 30px;
            padding: 25px;
            background: #f8f9ff;
            border-radius: 8px;
            border-left: 4px solid #667eea;
        }

        .section-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
        }

        .section-title i {
            margin-right: 8px;
            color: #667eea;
        }

        .layui-form-item {
            margin-bottom: 20px;
        }

        .layui-form-label {
            width: 120px;
            font-weight: 500;
            color: #333;
        }
        
        .layui-input-inline {
            width: 200px;
        }

        .layui-input, .layui-select, .layui-textarea {
            border-radius: 6px;
            border: 1px solid #e6e6e6;
            transition: all 0.3s;
        }

        .layui-input:focus, .layui-select:focus, .layui-textarea:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
        }

        .two-column {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
        }

        .btn-section {
            background: #f8f9fa;
            padding: 25px 40px;
            border-top: 1px solid #e9ecef;
            text-align: center;
        }

        .layui-btn {
            border-radius: 6px;
            font-weight: 500;
            padding: 12px 30px;
            margin: 0 10px;
            transition: all 0.3s;
        }

        .layui-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
        }

        .layui-btn-primary {
            background: #6c757d;
            border-color: #6c757d;
            color: white;
        }

        .layui-btn-primary:hover {
            background: #5a6268;
            border-color: #545b62;
        }

        /* 响应式设计 */
        @@media (max-width: 768px) {
            .two-column {
                grid-template-columns: 1fr;
                gap: 20px;
            }
            
            .form-content {
                padding: 20px;
            }
            
            .layui-input-inline {
                width: 100%;
            }
        }

        /* 加载动画 */
        .loading-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(255,255,255,0.9);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 9999;
            display: none;
        }

        .loading-spinner {
            width: 40px;
            height: 40px;
            border: 4px solid #f3f3f3;
            border-top: 4px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @@keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <!-- 加载动画 -->
    <div class="loading-overlay" id="loadingOverlay">
        <div class="loading-spinner"></div>
    </div>

    <div class="form-container">
        <!-- 表单头部 -->
        <div class="form-header">
            <h1 class="form-title">修改维修单</h1>
            <p class="form-subtitle">Update Repair Order Information</p>
        </div>

        <!-- 表单内容 -->
        <div class="form-content">
            <form class="layui-form" lay-filter="repairOrderForm">
                <!-- 基本信息 -->
                <div class="form-section">
                    <div class="section-title">
                        <i class="layui-icon layui-icon-form"></i>
                        基本信息
                    </div>
                    <div class="two-column">
                        <div class="layui-form-item">
                            <label class="layui-form-label">维修单编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="repairOrderCode" placeholder="维修单编号" autocomplete="off" class="layui-input" readonly>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">维修单名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="repairOrderName" placeholder="请输入维修单名称" autocomplete="off" class="layui-input" lay-verify="required">
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 设备信息 -->
                <div class="form-section">
                    <div class="section-title">
                        <i class="layui-icon layui-icon-engine"></i>
                        设备信息
                    </div>
                    <div class="two-column">
                        <div class="layui-form-item">
                            <label class="layui-form-label">设备编码</label>
                            <div class="layui-input-inline">
                                <input type="text" name="equipmentCode" placeholder="请输入设备编码" autocomplete="off" class="layui-input" lay-verify="required">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">设备名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="equipmentName" placeholder="请输入设备名称" autocomplete="off" class="layui-input" lay-verify="required">
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 日期信息 -->
                <div class="form-section">
                    <div class="section-title">
                        <i class="layui-icon layui-icon-date"></i>
                        日期信息
                    </div>
                    <div class="two-column">
                        <div class="layui-form-item">
                            <label class="layui-form-label">报修日期</label>
                            <div class="layui-input-inline">
                                <input type="text" name="warrantyDate" placeholder="请选择报修日期" autocomplete="off" class="layui-input" lay-verify="required" id="warrantyDate">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">维修完成日期</label>
                            <div class="layui-input-inline">
                                <input type="text" name="completionDate" placeholder="请选择维修完成日期" autocomplete="off" class="layui-input" id="completionDate">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">验收日期</label>
                            <div class="layui-input-inline">
                                <input type="text" name="acceptanceDate" placeholder="请选择验收日期" autocomplete="off" class="layui-input" id="acceptanceDate">
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 维修信息 -->
                <div class="form-section">
                    <div class="section-title">
                        <i class="layui-icon layui-icon-set"></i>
                        维修信息
                    </div>
                    <div class="two-column">
                        <div class="layui-form-item">
                            <label class="layui-form-label">维修结果</label>
                            <div class="layui-input-inline">
                                <select name="repairResults" lay-verify="">
                                    <option value="">请选择维修结果</option>
                                    <option value="维修完成">维修完成</option>
                                    <option value="部分完成">部分完成</option>
                                    <option value="无法维修">无法维修</option>
                                    <option value="待配件">待配件</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">单据状态</label>
                            <div class="layui-input-inline">
                                <select name="documentStatus" lay-verify="required">
                                    <option value="">请选择单据状态</option>
                                    <option value="0">待处理</option>
                                    <option value="1">已完成</option>
                                    <option value="2">已取消</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">维修人员</label>
                            <div class="layui-input-inline">
                                <input type="text" name="repairPersonnel" placeholder="请输入维修人员姓名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">验收人员</label>
                            <div class="layui-input-inline">
                                <input type="text" name="acceptancePersonnel" placeholder="请输入验收人员姓名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 隐藏字段 -->
                <input type="hidden" name="id" id="repairOrderId">
            </form>
        </div>

        <!-- 按钮区域 -->
        <div class="btn-section">
            <button type="submit" class="layui-btn" lay-submit lay-filter="submitForm">
                <i class="layui-icon layui-icon-ok"></i> 保存修改
            </button>
            <button type="button" class="layui-btn layui-btn-primary" id="cancelBtn">
                <i class="layui-icon layui-icon-return"></i> 返回列表
            </button>
        </div>
    </div>

    <script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>
    <script>
    layui.use(['form', 'layer', 'laydate'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var $ = layui.$;

        // 获取URL参数中的ID
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            return r != null ? unescape(r[2]) : null;
        }

        var repairOrderId = getUrlParam('id');
        if (!repairOrderId) {
            layer.msg('缺少维修单ID参数', {icon: 2});
            setTimeout(function(){
                window.location.href = '/DeviceLedger/RepairOrderView';
            }, 2000);
            return;
        }

        // 显示加载动画
        function showLoading() {
            $('#loadingOverlay').show();
        }

        // 隐藏加载动画
        function hideLoading() {
            $('#loadingOverlay').hide();
        }

        // 日期选择器
        laydate.render({
            elem: '#warrantyDate',
            type: 'date',
            format: 'yyyy-MM-dd'
        });

        laydate.render({
            elem: '#completionDate',
            type: 'date',
            format: 'yyyy-MM-dd'
        });

        laydate.render({
            elem: '#acceptanceDate',
            type: 'date',
            format: 'yyyy-MM-dd'
        });

        // 加载维修单数据
        function loadRepairOrderData() {
            showLoading();
            
            $.ajax({
                url: '/DeviceLedger/GetRepairOrderById',
                type: 'GET',
                data: { id: repairOrderId },
                success: function(result) {
                    hideLoading();
                    if (result.code === 0 && result.data) {
                        var data = result.data;
                        
                        // 填充表单数据
                        $('input[name="repairOrderCode"]').val(data.repairOrderCode || '');
                        $('input[name="repairOrderName"]').val(data.repairOrderName || '');
                        $('input[name="equipmentCode"]').val(data.equipmentCode || '');
                        $('input[name="equipmentName"]').val(data.equipmentName || '');
                        $('input[name="repairPersonnel"]').val(data.repairPersonnel || '');
                        $('input[name="acceptancePersonnel"]').val(data.acceptancePersonnel || '');
                        
                        // 处理日期字段
                        if (data.warrantyDate) {
                            var warrantyDate = new Date(data.warrantyDate);
                            $('input[name="warrantyDate"]').val(warrantyDate.toISOString().split('T')[0]);
                        }
                        if (data.completionDate) {
                            var completionDate = new Date(data.completionDate);
                            $('input[name="completionDate"]').val(completionDate.toISOString().split('T')[0]);
                        }
                        if (data.acceptanceDate) {
                            var acceptanceDate = new Date(data.acceptanceDate);
                            $('input[name="acceptanceDate"]').val(acceptanceDate.toISOString().split('T')[0]);
                        }
                        
                        // 设置下拉框值
                        $('select[name="repairResults"]').val(data.repairResults || '');
                        $('select[name="documentStatus"]').val(data.documentStatus || '');
                        
                        // 设置隐藏字段
                        $('#repairOrderId').val(data.id || '');
                        
                        // 重新渲染表单
                        form.render();
                    } else {
                        layer.msg(result.msg || '获取维修单数据失败', {icon: 2});
                        setTimeout(function(){
                            window.location.href = '/DeviceLedger/RepairOrderView';
                        }, 2000);
                    }
                },
                error: function() {
                    hideLoading();
                    layer.msg('网络错误，获取数据失败', {icon: 2});
                    setTimeout(function(){
                        window.location.href = '/DeviceLedger/RepairOrderView';
                    }, 2000);
                }
            });
        }

        // 页面加载时获取数据
        loadRepairOrderData();

        // 表单提交
        form.on('submit(submitForm)', function(data){
            var loading = layer.load(2, {content: '正在保存...'});
            
            // 处理表单数据
            var formData = $.extend({}, data.field);
            
            // 确保必填日期字段有默认值
            if (!formData.warrantyDate || formData.warrantyDate.trim() === '') {
                layer.close(loading);
                layer.msg('请选择报修日期', {icon: 2});
                return false;
            }
            
            // 处理可选日期字段，转换为ISO格式
            if (formData.warrantyDate && formData.warrantyDate.trim() !== '') {
                formData.warrantyDate = new Date(formData.warrantyDate + 'T00:00:00').toISOString();
            }
            if (formData.completionDate && formData.completionDate.trim() !== '') {
                formData.completionDate = new Date(formData.completionDate + 'T00:00:00').toISOString();
            } else {
                formData.completionDate = new Date('1900-01-01T00:00:00').toISOString();
            }
            if (formData.acceptanceDate && formData.acceptanceDate.trim() !== '') {
                formData.acceptanceDate = new Date(formData.acceptanceDate + 'T00:00:00').toISOString();
            } else {
                formData.acceptanceDate = new Date('1900-01-01T00:00:00').toISOString();
            }
            
            // 处理可选字符串字段
            if (!formData.repairResults || formData.repairResults.trim() === '') {
                formData.repairResults = '';
            }
            if (!formData.repairPersonnel || formData.repairPersonnel.trim() === '') {
                formData.repairPersonnel = '';
            }
            if (!formData.acceptancePersonnel || formData.acceptancePersonnel.trim() === '') {
                formData.acceptancePersonnel = '';
            }
            
            $.ajax({
                url: '/DeviceLedger/UpdateRepairOrder',
                type: 'POST',
                data: formData,
                headers: {
                    'RequestVerificationToken': $('input[name="__RequestVerificationToken"]').val()
                },
                success: function(result) {
                    layer.close(loading);
                    if (result.code === 0) {
                        layer.msg('维修单修改成功！', {icon: 1, time: 2000}, function(){
                            window.location.href = '/DeviceLedger/RepairOrderView';
                        });
                    } else {
                        layer.msg(result.msg || '修改失败，请检查输入信息', {icon: 2});
                    }
                },
                error: function() {
                    layer.close(loading);
                    layer.msg('网络错误，修改失败', {icon: 2});
                }
            });
            
            return false;
        });

        // 返回按钮
        $('#cancelBtn').on('click', function(){
            layer.confirm('确定要返回吗？未保存的修改将丢失。', {
                icon: 3,
                title: '确认返回'
            }, function(index){
                window.location.href = '/DeviceLedger/RepairOrderView';
                layer.close(index);
            });
        });
    });
    </script>
</body>
</html>
